/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package guiutilities;

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import org.jdesktop.application.ResourceMap;

/**
 *
 * @author tommy
 */
public class jSingleDoubleArgumentDialog extends JDialog {
	private boolean confirmed;
	private Double arg1;
	private JLabel jArg1Label, jAux;
	private JTextField jArg1Text;
	private JButton jConfirmOperation;
	
	
	public jSingleDoubleArgumentDialog(JFrame parent, ResourceMap resourceMap, String labelArg) {
		// Dialog for browsing and storing 2 images to perform operations among
		// them
		super(parent,"Supply Argument", true);
		
		getContentPane().setLayout(new SpringLayout());
		setPreferredSize(new Dimension(512,196));
		setMaximumSize(new Dimension(512,196));
		setResizable(false);
		setSize(512, 196);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		
		jArg1Label = new JLabel(labelArg + ": ");
		jAux = new JLabel();
		
		jArg1Text = new JTextField();
		jArg1Text.setMaximumSize(new Dimension(200, 27));
		jArg1Text.setPreferredSize(new Dimension(200, 27));
		

		jConfirmOperation = new JButton();
        jConfirmOperation.setText(resourceMap.getString("jConfirmOperation.text")); // NOI18N
        jConfirmOperation.setName("jConfirmOperation"); // NOI18N
		jConfirmOperation.setPreferredSize(new Dimension(50,50));
		jConfirmOperation.setMinimumSize(new Dimension(50,50));
        jConfirmOperation.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jConfirmOperationActionPerformed(evt);
            }
        });
		
		// Add components to Dialog
		getContentPane().add(jArg1Label);
		getContentPane().add(jArg1Text);
		getContentPane().add(jConfirmOperation);
		getContentPane().add(jAux);
		
		SpringUtilities.makeCompactGrid(getContentPane(), 2,2,6,6,6, 6);
	}
	
	public boolean isConfirmed() {
		return this.confirmed;
	}
	
	public double getInputArg() {
		return this.arg1;
	}
	
	private void jConfirmOperationActionPerformed(java.awt.event.ActionEvent evt) {                                                     
		boolean verified = false;

		try {
			this.arg1 = Double.parseDouble(jArg1Text.getText());

			if (this.arg1 < 0 ) {
				 // Fuerza que se capture una excepcion para volver a pedirle al usuario input
				throw new NumberFormatException();
			}
			verified = true;
		} catch (NumberFormatException e) {
			verified = false;
			JOptionPane.showMessageDialog(this,"Only positive integer numbers accepted");
		}
		
		if (verified) {
			this.dispose();
		}
	}
}
